import time

from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from time import sleep
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains

class Base():
    def __init__(self, driver):
        self.driver = driver

    def open_url(self, url):
        self.driver.get(url)
    #封装元素定位
    def find_element(self, loc):
        try:
            WebDriverWait(self.driver, 20, 0.5).until(lambda driver: driver.find_element(*loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)
    #封装点击
    def click(self, loc):
        self.find_element(loc).click()
    #封装输入
    def send_keys(self,loc,info):
        self.driver.find_element(*loc).send_keys(info)
    #封装文本
    def test(self,loc):
        return self.driver.find_element(loc).text
    #封装框架进
    def frame(self,loc):
        self.driver.switch_to.frame(self.find_element(loc))
    #封装退框架
    def tuiframe(self):
        self.driver.switch_to.default_content()
    #封装截图
    def time_jietu(self):
        et = time.strftime('%Y%m%d%H%M%S',time.localtime())
        self.driver.save_screenshot('...Png' + 'Img' + et + 'png')
    #封装滚动
    def execute_script(self):
        self.driver.execute_script('window.scrollTo(9999)')
    #封装弹框
    def prompt_accept(self):
        prompt=self.driver.switch_to.alert
        prompt.send_keys('弹出')
        prompt.dismiss()
    #封装切窗口
    def window_handles(self):
        self.driver.switch_to.window(self.driver.window_handles[-1])
    #封装鼠标双击
    def double_click(self,loc):
        ActionChains(self.driver).double_click(self.find_element(loc)).perform()
    #鼠标右击
    def context_click(self,loc):
        ActionChains(self.driver).context_click(self.find_element(loc)).perform()
    #封装键盘TAB
    def keys_TAB(self,loc):
        self.send_keys(loc,Keys.TAB)

    # 封装键盘ENTER
    def keys_ENTER(self, loc):
        self.send_keys(loc, Keys.ENTER)

    #封装浏览器窗口操作
    def quite(self):
        self.driver.quit()
